package com.lantosec.hmrp.modules.internal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.shiro.TokenUtil;
import com.lantosec.hmrp.modules.internal.entity.*;
import com.lantosec.hmrp.modules.internal.mapper.InternalTaskMapper;
import com.lantosec.hmrp.modules.internal.service.IInternalTaskService;
import com.lantosec.hmrp.modules.internal.vo.*;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评价任务 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-04-23
 */
@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class InternalTaskServiceImpl extends ServiceImpl<InternalTaskMapper, InternalTask> implements IInternalTaskService {

    @Autowired
    private InternalNoteServiceImpl internalNoteService;

    @Override
    public List<InternalTaskDetailVO> getParam(Integer internalTaskId) {
        //获取考评任务的考评方案
        InternalTask internalTask = this.getById(internalTaskId);
        //获取考评方案的被考评方式
        InternalPlan internalPlan = baseMapper.getInternalPlan(internalTask.getInternalPlanId());
        Integer targetMode = internalPlan.getTargetMode();
        //获取当前用户id和名称
        String executorUserId = TokenUtil.getUserId();
        //查询被考评科室
        List<String> deptId = baseMapper.getTargetDeptId(internalTask.getInternalPlanId());
        List<InternalTaskDetailVO> internalTaskDetailList = new ArrayList<>();
        //当考评方式是1，则被考评方是科室
        if (targetMode == 1) {
            //internalTaskDetailList = getInternalTaskDetailVOS(internalTaskId, internalPlan, targetMode, deptId);
            internalTaskDetailList = baseMapper.getDeptInternalTaskDetail(internalTaskId, deptId,executorUserId);
        }
        //当考评方式是3，则被考评方是人员
        if (targetMode == 3) {
            //internalTaskDetailList = getInternalTaskDetailVOS(internalTaskId, internalPlan, targetMode, executorUserId);
            internalTaskDetailList = baseMapper.getInternalTaskDetail(internalTaskId, executorUserId);
        }

        return internalTaskDetailList;
    }

    @Override
    public List<InternalTypeVO> getInternalTaskDetailVO(Integer internalTaskId, Integer internalPlanId, Integer targetMode, String targetId){
        List<InternalTypeVO> internalTypeVOList = new ArrayList<>();
        List<InternalNote> internalNoteList = new ArrayList<>();
        //获得当前用户ID
        String userId = TokenUtil.getUserId();
        if (targetMode == 1) {
            //查询出每个被考评方已考评的项目
            internalNoteList = internalNoteService.list(new QueryWrapper<InternalNote>().eq(InternalNote.ColName.internal_task_id.toString(), internalTaskId)
                    .eq(InternalNote.ColName.target_dept_id.toString(), targetId).eq(InternalNote.ColName.executor_user_id.toString(),userId));
            internalTypeVOList = getInternalHistor(internalTaskId, internalPlanId, targetMode, internalNoteList, targetId);
            return internalTypeVOList;
        }
        else{
            //查询出每个被考评方已考评的项目
            internalNoteList = internalNoteService.list(new QueryWrapper<InternalNote>().eq(InternalNote.ColName.internal_task_id.toString(), internalTaskId)
                    .eq(InternalNote.ColName.target_user_id.toString(), targetId));
            internalTypeVOList = getInternalHistor(internalTaskId, internalPlanId, targetMode, internalNoteList, targetId);
            return internalTypeVOList;
        }
    }

    private List<InternalTypeVO> getInternalHistor(Integer internalTaskId, Integer internalPlanId, Integer targetMode, List<InternalNote> internalNoteList, String targetId) {
        List<InternalTypeVO> internalTypeVOList = new ArrayList<>();
        //如果考评记录不为空，则返回考评记录
        if (null != internalNoteList && internalNoteList.size() > 0) {
            //查询出被考评对象的考评项目分类
            internalTypeVOList = baseMapper.getIntenalType(internalTaskId, targetMode, targetId);
            for (InternalTypeVO vo : internalTypeVOList) {
                Integer internalTypeId = vo.getInternalTypeId();
                List<InternalNoteVO> internalNoteVOList = baseMapper.getInternalNote(internalTaskId, targetMode, targetId, internalTypeId,internalPlanId);
                vo.setInternalNoteVOList(internalNoteVOList);
            }
            return internalTypeVOList;
            //如果考评记录为空，则返回考评项目
        } else {
            //查询出考评任务下的考评项目分类
            internalTypeVOList = baseMapper.getInternalType(internalPlanId);
            internalTypeVOList.forEach(parm1 -> {
                //获取考评方案下的考评项目
                List<InternalPlanItem> internalPlanItemList = baseMapper.getInternalPlanItem(parm1.getInternalTypeId(), internalPlanId);
                //查询考评方案的考评等级
                List<InternalPlanLevel> internalPlanLevelList = baseMapper.getInternalPlanLevel(internalPlanId);
                internalPlanItemList.forEach(parm2 -> {
                    parm2.setInternalPlanLevelList(internalPlanLevelList);
                });
                parm1.setInternalPlanItemList(internalPlanItemList);
            });
            return internalTypeVOList;
        }
    }

    @Override
    public List<InternalTypeVO> startInternal(Integer internalPlanId) {
        //查询出考评任务下的考评项目分类
        List<InternalTypeVO> internalTypeVOList = baseMapper.getInternalType(internalPlanId);
        internalTypeVOList.forEach(parm -> {
            //获取考评方案下的考评项目
            List<InternalPlanItem> internalPlanItemList = baseMapper.getInternalPlanItem(parm.getInternalTypeId(), internalPlanId);
            //查询考评方案的考评等级
            List<InternalPlanLevel> internalPlanLevelList = baseMapper.getInternalPlanLevel(internalPlanId);
            internalPlanItemList.forEach(parm1 -> {
                parm1.setInternalPlanLevelList(internalPlanLevelList);
            });
            parm.setInternalPlanItemList(internalPlanItemList);
        });

        return internalTypeVOList;
    }

    @Override
    public List<InternalTaskVO> getNotFinishAll(PageVO<InternalTaskVO> page, Integer internalPlanType, String taskTitle, String startDate, String endDate, Integer state) {
        List<InternalTaskVO> internalTaskVOList = baseMapper.getNotFinishAll(page, internalPlanType, taskTitle, startDate, endDate, state);
        page.setRecords(internalTaskVOList);
        return internalTaskVOList;
    }

    @Override
    public List<InternalTaskVO> getFinishAll(PageVO<InternalTaskVO> page, Integer internalPlanType, String taskTitle, String startDate, String endDate, Integer state) {
        List<InternalTaskVO> internalTaskVOList = baseMapper.getFinishAll(page, internalPlanType, taskTitle, startDate, endDate, state);
        page.setRecords(internalTaskVOList);
        return internalTaskVOList;
    }

    @Override
    public List<InternalTaskVO> getNotFinish(PageVO<InternalTaskVO> page, Integer internalPlanType,
                         String taskTitle, String startDate, String endDate, Integer state,Integer permissionsType) {
        if(null == permissionsType){
            permissionsType = 1;
        }
        //获取当前用户id和名称
        String executorUserId = TokenUtil.getUserId();
        String deptId = TokenUtil.getDeptId();
        List<InternalTaskVO> internalTaskVOList = null;
        //如果是科室考评
        if (permissionsType == 2) {
            //查询当前用户的部门
            String executorDeptId = TokenUtil.getDeptId();
            internalTaskVOList = baseMapper.getNotFinishDept(page, internalPlanType,taskTitle, startDate, endDate, executorDeptId);
            page.setRecords(internalTaskVOList);
            return internalTaskVOList;
        }
        //如果是个人考评
        if(permissionsType == 1){
            //获得当前用户id
            String userId = TokenUtil.getUserId();
            internalTaskVOList = baseMapper.getNotFinishUser(page, internalPlanType, taskTitle, startDate, endDate, userId);
            page.setRecords(internalTaskVOList);
            return  internalTaskVOList;
        }
        //如果是全院考评
        else{
            internalTaskVOList = baseMapper.getNotFinishAlls(page,internalPlanType,taskTitle,startDate,endDate);
            page.setRecords(internalTaskVOList);
            return internalTaskVOList;
        }
    }

    @Override
    public List<InternalTaskVO> getFinish(PageVO<InternalTaskVO> page, Integer internalPlanType,
                              String taskTitle, String startDate, String endDate, Integer state,Integer permissionsType) {
        if(null == permissionsType){
            permissionsType = 1;
        }
        List<InternalTaskVO> internalTaskVOList = null;
        if (permissionsType == 2) {
            String executorDeptId = TokenUtil.getDeptId();
            internalTaskVOList = baseMapper.getFinishDept(page, internalPlanType,taskTitle, startDate, endDate, executorDeptId);
            page.setRecords(internalTaskVOList);
            return internalTaskVOList;
        }
        if (permissionsType == 1) {
            //获取当前用户id和名称
            String userId = TokenUtil.getUserId();
            internalTaskVOList = baseMapper.getFinishUser(page, internalPlanType, taskTitle, startDate, endDate, userId);
            page.setRecords(internalTaskVOList);
            return internalTaskVOList;
        }
        else{
            internalTaskVOList = baseMapper.getFinishAlls(page,internalPlanType,taskTitle,startDate,endDate);
            page.setRecords(internalTaskVOList);
            return internalTaskVOList;
        }
    }

    @Override
    public Map<String,Object> getUserTaskDetail(Integer internalTaskId, Integer targetMode) {
        Map<String,Object> map = new HashMap<>();
        map = getInternalTaskInfo(internalTaskId);
        //查询我的考评任务被考评对象
        String userId = TokenUtil.getUserId();
        List<InternalTaskDetailVO> internalTaskDetails = baseMapper.getInternalTaskTarget(internalTaskId,targetMode,userId);
        map.put("internalTaskDetails",internalTaskDetails);
        //查询我的考评任务的已完成未完成详情
        List<InternalTaskDetailVO> internalTaskDetailFinish = baseMapper.getInternalTaskFinish(internalTaskId,targetMode,userId);
        List<InternalTaskDetailVO> internalTaskDetailNotFinish = baseMapper.getInternalTaskNotFinish(internalTaskId,targetMode,userId);
        map.put("taskFinish",internalTaskDetailFinish);
        map.put("taskNotFinish",internalTaskDetailNotFinish);
        return map;
    }

    @Override
    public Map<String,Object> getDeptTaskDetail(Integer internalTaskId) {
        Map<String,Object> map = new HashMap<>();
        map = getInternalTaskInfo(internalTaskId);
        //查询科室考评任务被考评对象
        String deptId = TokenUtil.getDeptId();
        List<InternalTaskExecutorVO> internalTaskExecutors = baseMapper.getInternalTaskExecutor(internalTaskId,deptId);
        map.put("internalTaskExecutors",internalTaskExecutors);
        //查询科室考评任务的已完成未完成详情
        List<InternalTaskDetailVO> internalTaskDetailFinishAll = baseMapper.getInternalTaskDeptFinish(internalTaskId,deptId);
        List<InternalTaskDetailVO> internalTaskDetailNotFinishAll = baseMapper.getInternalTaskDeptNotFinish(internalTaskId,deptId);
        map.put("taskFinish",internalTaskDetailFinishAll);
        map.put("taskNotFinish",internalTaskDetailNotFinishAll);
        return map;
    }

    @Override
    public Map<String,Object> getAllTaskDetail(Integer internalTaskId) {
        Map<String,Object> map = new HashMap<>();
        map = getInternalTaskInfo(internalTaskId);
        //查询全院考评任务的被考评对象
        List<InternalTaskExecutorVO> internalTaskExecutors = baseMapper.getInternalTaskExecutor(internalTaskId,null);
        map.put("internalTaskExecutors",internalTaskExecutors);
        //查询全院考评任务的已完成未完成详情
        List<InternalTaskDetailVO> internalTaskDetailFinishAll = baseMapper.getInternalTaskFinishAll(internalTaskId);
        List<InternalTaskDetailVO> internalTaskDetailNotFinishAll = baseMapper.getInternalTaskNotFinishAll(internalTaskId);
        map.put("taskFinish",internalTaskDetailFinishAll);
        map.put("taskNotFinish",internalTaskDetailNotFinishAll);
        return map;
    }

    /**
     * 查询考评任务基本信息
     * @param internalTaskId
     * @return
     */
    private Map<String,Object> getInternalTaskInfo(Integer internalTaskId) {
        Map<String,Object> map = new HashMap<>();
        //查询考评任务基本信息
        InternalTask internalTask = this.getById(internalTaskId);
        map.put("internalTask",internalTask);
        //查询考评任务选择的项目
        Integer internalPlanId = internalTask.getInternalPlanId();
        List<InternalPlanItem> internalPlanItems = baseMapper.getInternalPlanItem(null,internalPlanId);
        map.put("internalPlanItems",internalPlanItems);
        //查询考评任务等级
        List<InternalPlanLevel> internalPlanLevels = baseMapper.getInternalPlanLevel(internalPlanId);
        map.put("internalPlanLevels",internalPlanLevels);
        return map;
    }
}
